<?php
// +----------------------------------------------------------------------
// | 多客怪兽框架多平台开发框架 [ 以下开发工具自动生成-版权勿删 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~2024 http://www.dkyx.cc All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘泓宾（514660094@qq.com）
// +----------------------------------------------------------------------

namespace addons\cloud_market\model;

use think\model\concern\SoftDelete;

/**
 * 开发者管理模型文件
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 */
class CloudMarketDeveloper extends CloudMarketBaseModel
{
    // 配置软删除、启用回收站
    use SoftDelete;
    protected $deleteTime = "delete_time";

    // 字段自动类型转换
    protected $type = array(
        'rule' => 'json',
        'kefu' => 'json'
    );
    // 数据输出需要追加的属性
    protected $append = array(

    );
    public function __construct(array $data = [])
    {
        parent::__construct($data);
    }


    /**
     * 关联CloudMarketUser模型
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(CloudMarketUser::class, 'user_id', 'id');
    }


    public function getLogoAttr($val)
    {
        // 如果值为空，直接返回
        if (empty($val)) {
            return $val;
        }

        // 检查是否已经包含 http 或 https 前缀
        if (!preg_match('/^http(s)?:\/\//i', $val)) {
            // 添加前缀
            $val = 'https://dkyx-1251560393.cos.ap-chengdu.myqcloud.com' . $val;
            $this->logo = $val;
            $this->save();
        }

        return $val;
    }

    public function getRuleAttr($val)
    {
        if($val == "all"){
            return ['all'];
        }else{
            return json_decode($val, true);
        }
    }
}
